#ifndef GTL_ENGINE_RESOURCE_HEADER_GUARD
#define GTL_ENGINE_RESOURCE_HEADER_GUARD

#include "gtlCore.h"
#include <GTL/gtlAutomember.h>

namespace GTL { namespace Engine {

	class Resource : public GTL::AutoMembers::AutoMember
	{
	public:
		int FileSizeCache;
		Resource()
		{
			FileSizeCache = -1;
			AddAutoMemberToPostOrderList();
		}

		virtual int GetBaseType() { return 'reso'; }
		virtual int GetType() { return 'reso'; }

		virtual void OnLoad() = 0;
		virtual void LoadFrom(const char* src) = 0;
		virtual void Unload() = 0;

		virtual const char* GetFileName() const = 0;
		virtual const char* GetName() const = 0;
	};

	class ImageResource : public Resource
	{
		ImageRef* image;
	public:
		virtual int GetType() { return 'imag'; }

		virtual void LoadFrom(const char* src)
		{
			//printf("Loading image %s [%s]\n", GetName(), src);
			image = Backend->LoadImage(src, true);
			if (!image)
				Backend->LogError("Could not load image from `%s`", src);
		}

		virtual void Unload()
		{
			if (image)
				Backend->DestroyImage(image);
			/*
			if (image)
			{
				//printf("Unloading image %s\n", GetName());
				//image.destroy();
				image = 0;
			}*/
		}

		operator ImageRef*() { return image; }
		//operator Image&() { return image; }
		ImageRef* operator->() { return image; }
	};

	class FontResource : public Resource
	{
		FontRef* image;
	public:
		virtual int GetType() { return 'font'; }

		virtual void LoadFrom(const char* src)
		{
			image = Backend->LoadFont(src);
			if (!image)
				Backend->LogError("Could not load font from `%s`", src);
		}

		virtual void Unload()
		{
			if (image)
				Backend->DestroyFont(image);
			/*
			if (image)
			{
				//printf("Unloading image %s\n", GetName());
				//image.destroy();
				image = 0;
			}*/
		}

		operator FontRef*() { return image; }
		FontRef* operator->() { return image; }
	};

	#define resource(itype, iname, isrc) class iname##_rsr : public GTL::Engine::itype##Resource \
	{  \
		virtual const char* GetFileName() const { return isrc; } \
		virtual const char* GetName() const { return #iname ; } \
		virtual void OnLoad() { LoadFrom(isrc); }  \
	} iname;
/*

	class RSound : public Resource
	{
		Sound sound;
	public:
		virtual int GetType() { return 'SOUN'; }

		virtual void LoadFrom(const char* src)
		{
			printf("Loading sound %s [%s]\n", GetName(), src);
			sound.load(src);
			if (!sound.is_loaded())
				printf("[ERROR] ========> !! could not load %s\n", GetName());
		}

		virtual void Unload()
		{
			if (sound.is_loaded())
			{
				printf("Unloading sound %s\n", GetName());
				sound.destroy();
			}
		}

		operator Sound*() { return &sound; }
		operator Sound&() { return sound; }
		Sound* operator->() { return &sound; }
	};

	#define LoadSound(iname, isrc) class iname##_rsr : public RSound \
	{  \
		virtual const char* GetFileName() const { return isrc; } \
		virtual const char* GetName() const { return #iname ; } \
		virtual void OnLoad() { LoadFrom(isrc); }  \
	} iname;
*/
}}

#endif // gtlResource_h__